package com.haoshuang.zhouzhoubang.ui.page.account.login.code

import android.os.Bundle
import com.haoshuang.zhouzhoubang.config.Config
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.FragmentCodeLoginBinding
import com.haoshuang.zhouzhoubang.ui.page.account.forget.ForgetActivity
import com.haoshuang.zhouzhoubang.ui.page.account.register.RegisterActivity
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.main.MainActivity
import com.haoshuang.zhouzhoubang.utils.PreferenceUtils
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.CountDownWorker
import com.mufeng.libs.utils.finishPage
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.WXAPIFactory


/**
 *
 */
class CodeLoginFragment : BaseVMFragment<CodeLoginViewModel, FragmentCodeLoginBinding>() {

    companion object {
        fun getInstance(): CodeLoginFragment {
            val fragment = CodeLoginFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.tvForget.clickWithTrigger {
            startPage<ForgetActivity>()
        }
        binding.tvRegister.clickWithTrigger {
            startPage<RegisterActivity>()
        }
        binding.tvPrivacyPolicy.clickWithTrigger {
            startPage<HelpDetailActivity>("title" to "隐私政策", "url" to URLConstants.privacyPolicy)
        }
        binding.tvGetCode.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            vm.sendSMS(phone)
        }
        binding.tvLogin.clickWithTrigger {
            val phone = binding.etPhone.text.toString()
            if (phone.isEmpty()) {
                toast("请输入手机号")
                return@clickWithTrigger
            }
            if (phone.length != 11) {
                toast("请输入正确的手机号")
                return@clickWithTrigger
            }
            val code = binding.etCode.text.toString()
            if (code.isEmpty()) {
                toast("请输入验证码")
                return@clickWithTrigger
            }
            if (!binding.cbAgreement.isChecked) {
                toast("请阅读并同意《隐私政策》")
                return@clickWithTrigger
            }
            vm.login(phone, code)
        }

        binding.llWechatLogin.clickWithTrigger {
            if (!binding.cbAgreement.isChecked) {
                toast("请阅读并同意《隐私政策》")
                return@clickWithTrigger
            }
            // 微信登录
            PreferenceUtils.putString("wx_type", "2")
            val mWxApi = WXAPIFactory.createWXAPI(
                mActivity,
                Config.WX_ID,
                false
            )
            // 将该app注册到微信
            mWxApi.registerApp(Config.WX_ID)
            if (!mWxApi.isWXAppInstalled) {
                toast("微信未安装,请先安装微信")

            } else {
                val req = SendAuth.Req()
                req.scope = "snsapi_userinfo"
                req.state = "diandi_wx_login"
                mWxApi.sendReq(req)
            }
        }
    }


    override fun initData() {

    }

    override fun lazyLoadData() {

    }

    override fun startObserve() {
        vm.sendSMSData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.tvGetCode.isEnabled = false
                CountDownWorker(this@CodeLoginFragment, onChange = {
                    binding.tvGetCode.text = "${it}S"
                }, onFinish = {
                    binding.tvGetCode.isEnabled = true
                    binding.tvGetCode.text = "重新获取"
                }).start()
            }
        }

        vm.loginData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("登录成功")
                startPage<MainActivity>()
                AccountUtils.isTokenErrorJumpLoginPage = true
                AccountUtils.user = it?.data
                AccountUtils.token = it?.data?.token ?: ""
                finishPage()
            }
        }
    }

}